package me.zhengjie.kwService.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.constant.KwConstant;
import me.zhengjie.kwService.PhoneStaffService;
import me.zhengjie.mapper.ExamMapper;
import me.zhengjie.mapper.ExamRoomMapper;
import me.zhengjie.mapper.ExamStudentMapper;
import me.zhengjie.mapper.ExamTeacherMapper;
import me.zhengjie.pojo.dto.PageDto;
import me.zhengjie.pojo.dto.StaffCheckDto;
import me.zhengjie.pojo.entity.*;
import me.zhengjie.pojo.vo.FindAllExamRoomVo;
import me.zhengjie.pojo.vo.RoomTearcherStudentVo;
import me.zhengjie.pojo.vo.StudentVo;
import me.zhengjie.pojo.vo.TearcherVo;
import me.zhengjie.util.AssertUtils;
import me.zhengjie.util.BaseResponse;
import me.zhengjie.util.RedisUtils;
import me.zhengjie.util.ruoyiexcel.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author Long
 * @date 2021/7/7 9:45
 */
@Service
public class PhoneStaffServiceImpl implements PhoneStaffService {

    @Autowired
    private ExamMapper examMapper;
    @Autowired
    private ExamRoomMapper examRoomMapper;
    @Autowired
    private ExamStudentMapper examStudentMapper;
    @Autowired
    private ExamTeacherMapper examTeacherMapper;

    @Override
    public IPage<Exam> getExam(PageDto dto, HttpServletRequest request) {
        IPage<Exam> page = new Page<>(dto.getCurrent(), dto.getSize());
        String userToken = request.getHeader(KwConstant.USERTOKEN);
        String loginJson = RedisUtils.INSTANCE.get(userToken);
        MasterTearcher masterTearcher = JSON.parseObject(loginJson, MasterTearcher.class);
        AssertUtils.isNotNull(masterTearcher, "登录信息为空");
        AssertUtils.isNotNull(masterTearcher.getId(), "教师id为空");
        page = examTeacherMapper.findStaffExam(page, masterTearcher.getId());
        return page;
    }

    @Override
    public FindAllExamRoomVo findAllExamRoom(Integer id, HttpServletRequest request) {

        MasterTearcher masterTearcher = RedisUtils.INSTANCE.getLoginInfo(request.getHeader(KwConstant.USERTOKEN), MasterTearcher.class);
        AssertUtils.isNotNull(id, "考试id不可为空");
        Exam exam = examMapper.selectById(id);
        FindAllExamRoomVo findAllExamRoomVo = new FindAllExamRoomVo();
        BeanUtils.copyProperties(exam, findAllExamRoomVo);

        /*查询出当前监考人员点击的考试所要校验的考场id*/
        QueryWrapper<ExamTeacher> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id", masterTearcher.getId())
                .eq("exam_id", id);
        List<ExamTeacher> examTeachers = examTeacherMapper.selectList(wrapper);
        if(examTeachers.isEmpty()){
            return findAllExamRoomVo;
        }
        Set<Integer> examRoomIds = new HashSet<>();
        for (ExamTeacher examTeacher : examTeachers) {
            examRoomIds.add(examTeacher.getExamRoomId());
        }

        /*根据考场id查出所有考场信息*/
        QueryWrapper<ExamRoom> examRoomQueryWrapper = new QueryWrapper<>();
        examRoomQueryWrapper.in("id", examRoomIds);
        List<ExamRoom> examRooms = examRoomMapper.selectList(examRoomQueryWrapper);
        LinkedList<RoomTearcherStudentVo> list = new LinkedList<>();
        if (examRooms.isEmpty()) {
            return findAllExamRoomVo;
        }
        for (ExamRoom examRoom : examRooms) {
            RoomTearcherStudentVo vo = new RoomTearcherStudentVo();
            LinkedList<StudentVo> studentVos = new LinkedList<>();
            LinkedList<TearcherVo> tearcherVos = new LinkedList<>();
            BeanUtils.copyProperties(examRoom, vo);
            /*查询出该考场的学生添加到vo中*/
            QueryWrapper<ExamStudent> examStudentQueryWrapper = new QueryWrapper<>();
            examStudentQueryWrapper.eq("exam_room_id", examRoom.getId());
            List<ExamStudent> examStudentList = examStudentMapper.selectList(examStudentQueryWrapper);
            if (!examStudentList.isEmpty()) {
                for (ExamStudent examStudent : examStudentList) {
                    StudentVo studentVo = new StudentVo();
                    BeanUtils.copyProperties(examStudent, studentVo);
                    studentVos.add(studentVo);
                }
                vo.setStudentVos(studentVos);
            }
            /*查询出该场考试的监考老师，添加到vo中*/
            QueryWrapper<ExamTeacher> examTeacherQueryWrapper = new QueryWrapper<>();
            examTeacherQueryWrapper.eq("exam_room_id", examRoom.getId())
                    .like("identity", "0");
            List<ExamTeacher> examTeacherList = examTeacherMapper.selectList(examTeacherQueryWrapper);
            if (!examTeacherList.isEmpty()) {
                for (ExamTeacher examTeacher : examTeacherList) {
                    TearcherVo tearcherVo = new TearcherVo();
                    BeanUtils.copyProperties(examTeacher, tearcherVo);
                    tearcherVos.add(tearcherVo);
                }
                vo.setTearcherVos(tearcherVos);
            }
            list.add(vo);
        }
        findAllExamRoomVo.setVos(list);

        return findAllExamRoomVo;
    }

    @Override
    public RoomTearcherStudentVo findOneExamRoom(Integer id) {
        AssertUtils.isNotNull(id, "考场id不可为空");
        RoomTearcherStudentVo vo = examRoomMapper.findOneExamRoom(id);
        return vo;
    }

    @Override
    public BaseResponse check(StaffCheckDto dto, HttpServletRequest request) {
        AssertUtils.isNotNull(dto, "校验信息不可为空");
        MasterTearcher masterTearcher = RedisUtils.INSTANCE.getLoginInfo(request.getHeader(KwConstant.USERTOKEN), MasterTearcher.class);
        AssertUtils.isNotNull(masterTearcher, "登录信息为空");
        if (!masterTearcher.getIdentity().contains("1")) {
            System.out.println("非考务人员不可校验考场");
            return BaseResponse.failed("非考务人员不可校验考场");
        }
        UpdateWrapper<ExamRoom> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", dto.getId())
                .set(StringUtils.isNotBlank(dto.getCheckRemark()), "check_remark", dto.getCheckRemark())
                .set("status", "0".equals(dto.getStatus()) ? "0" : "1")
                .set("update_time", new Date());
        examRoomMapper.update(null, wrapper);
        return BaseResponse.ok("校验成功");
    }
}
